#include "CGESprite.h"
//#include "sprig.h"
#include "SDL_rotozoom.h" 


CGESprite::CGESprite(CGESurface surface,int x,int y,int texX,int texY,int texW,int texH)
:m_texture(surface),
m_X(x),
m_Y(y)
{

	m_ColorKey.r = m_ColorKey.g = m_ColorKey.b = m_ColorKey.unused = 0;

	m_texture.m_RectSur.x = texX;
	m_texture.m_RectSur.y = texY;
	m_texture.m_RectSur.w = texW;
	m_texture.m_RectSur.h = texH;
	m_Width = texW;
	m_Height = texH; 
}

CGESprite::~CGESprite(void)
{

}
void CGESprite::SetScale(float scaleW, float scaleH)
{
	m_Width	 *= scaleW;
	m_Height *= scaleH;
	m_texture.m_RectSur.x *= scaleW;
	m_texture.m_RectSur.y *= scaleH;
	m_texture.m_RectSur.w = m_Width;
	m_texture.m_RectSur.h = m_Height;
	//m_texture.m_Surface = SPG_Scale(m_texture.m_Surface,scaleW,scaleH);
	m_texture.m_Surface = zoomSurface(m_texture.m_Surface,scaleW,scaleH,SMOOTHING_ON);    
}
//crane: This will reduce performance seriously.For a sprite,cause 4 times FPS.
void CGESprite::SetColorKey(int red,int green,int blue)
{
	m_ColorKey.r = red;
	m_ColorKey.g = green;
	m_ColorKey.b = blue;

	Uint32 colorkey = SDL_MapRGB( m_texture.m_Surface->format, m_ColorKey.r, m_ColorKey.g, m_ColorKey.b );

	//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
	SDL_SetColorKey( m_texture.m_Surface, SDL_SRCCOLORKEY, colorkey );
}
void CGESprite::RenderSpr()
{
	//Make a temporary rectangle to hold the offsets
	SDL_Rect offset;

	//Give the offsets to the rectangle
	offset.x = m_X;
	offset.y = m_Y;

	//Blit the surface
	SDL_BlitSurface( m_texture.m_Surface, &m_texture.m_RectSur, CGESpriteMgr->GetScreenSurface(), &offset );
}

CGEAnim::CGEAnim(CGESurface surface,int frames,int x,int y,int texX,int texY,int texW,int texH)
:CGESprite(surface,x,y,texX,texY,texW,texH)
{
	m_Frames = frames;
}

CGEAnim::~CGEAnim(void)
{
}

void CGEAnim::Update(float seconds)
{

}


CGESpriteManager* CGESpriteManager::Instance()
{
	static CGESpriteManager instance;

	return &instance;
}
void CGESpriteManager::Flip()
{
	SDL_Flip(m_Screen);
}

void CGESpriteManager::AddSprite(CGESprite* sprite)
{
	for(vector<CGESprite*>::iterator it = m_VecSprite.begin();it != m_VecSprite.end() ;it++)
	{
		if((*it) == sprite)
		{
			return;
		}
	}
	m_VecSprite.push_back(sprite);
}


void CGESpriteManager::DelSprite(CGESprite* sprtie)
{
	for(vector<CGESprite*>::iterator it = m_VecSprite.begin();it != m_VecSprite.end() ;it++)
	{
		if((*it) == sprtie)
		{
			//crane:It is dangerous for deleting sprite dynamically.
			//delete (*it);
			m_VecSprite.erase(it);
			return;
		}
	}
}

void CGESpriteManager::Render()
{
	for(vector<CGESprite*>::iterator it = m_VecSprite.begin();it != m_VecSprite.end();it++)
	{
		(*it)->RenderSpr();
	}
}

CGESurface* CGESpriteManager::FindSurface(string filename)
{
	for(vector<CGESurface*>::iterator it = m_VecSurface.begin();it != m_VecSurface.end();it++)
	{
		if((*it)->m_Name == filename)
		{
			return (*it);
		}
	}
	return NULL;
}

void CGESpriteManager::DelSurface(string filename)
{
	for(vector<CGESurface*>::iterator it = m_VecSurface.begin();it != m_VecSurface.end();it++)
	{
		if((*it)->m_Name == filename)
		{
			SDL_FreeSurface((*it)->m_Surface);
			m_VecSurface.erase(it);	
			return; 
		}
	}
}

void CGESpriteManager::DelAll()
{
	for(vector<CGESprite*>::iterator it = m_VecSprite.begin();it != m_VecSprite.end() ;it++)
	{
		delete (*it);
	}
	for(vector<CGESurface*>::iterator it = m_VecSurface.begin();it != m_VecSurface.end();it++)
	{
		SDL_FreeSurface((*it)->m_Surface);
		delete (*it);
	}
	
	m_VecSurface.clear();	
	m_VecSprite.clear();
}

CGESurface* CGESpriteManager::LoadImage(string filename,VideoModule vm)
{

	//Temporary storage for the image that's loaded
	SDL_Surface* loadedImage = NULL;

	if(CGESpriteMgr->FindSurface(filename) == NULL)
	{
		CGESurface*	 optImage = new CGESurface();
		if(vm == vm_SDLImage)
		{
			//Load the image using SDL_image
			loadedImage = IMG_Load( filename.c_str() );
		}
		else if (vm == vm_SDL)
		{
			//Load the image
			loadedImage = SDL_LoadBMP( filename.c_str() );
		}
		//If nothing went wrong in loading the image
		if( loadedImage != NULL )
		{
			//Create an optimized image
			optImage->m_Surface =  SDL_DisplayFormatAlpha( loadedImage );
			optImage->m_RectSur = optImage->m_Surface->clip_rect;//crane:it is good?
			optImage->m_Name = filename;

			//Free the old image
			SDL_FreeSurface( loadedImage );

			////If the image was optimized just fine
			//if( optImage->m_Surface != NULL )
			//{
			//	//Map the color key
			//	Uint32 colorkey = SDL_MapRGB( optImage->m_Surface->format, 0, 0, 0 );
			//	//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
			//	SDL_SetColorKey( optImage->m_Surface, SDL_SRCCOLORKEY, colorkey );
			//}

			m_VecSurface.push_back(optImage);

			return optImage;
		}
		else
		{
			return NULL;
		}
	}
	else 
	{
		return CGESpriteMgr->FindSurface(filename);
	}
	
}
  